package com.ruoyi.park.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.ruoyi.common.core.utils.DateUtils;
import cn.hutool.core.util.IdUtil;
import com.ruoyi.park.domain.vo.MaterialReportVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.park.mapper.MaMaterialReportMapper;
import com.ruoyi.park.domain.MaMaterialReport;
import com.ruoyi.park.service.IMaMaterialReportService;

/**
 * 物资月报Service业务层处理
 *
 * @author felix
 * @date 2022-06-12
 */
@Service
public class MaMaterialReportServiceImpl implements IMaMaterialReportService {
    @Autowired
    private MaMaterialReportMapper maMaterialReportMapper;

    /**
     * 查询物资月报
     *
     * @param id 物资月报ID
     * @return 物资月报
     */
    @Override
    public MaMaterialReport selectMaMaterialReportById(String id) {
        return maMaterialReportMapper.selectMaMaterialReportById(id);
    }

    /**
     * 查询物资月报列表
     *
     * @param maMaterialReport 物资月报
     * @return 物资月报
     */
    @Override
    public List<MaMaterialReport> selectMaMaterialReportList(MaMaterialReport maMaterialReport) {
        return maMaterialReportMapper.selectMaMaterialReportList(maMaterialReport);
    }

    /**
     * 新增物资月报
     *
     * @param maMaterialReport 物资月报
     * @return 结果
     */
    @Override
    public int insertMaMaterialReport(MaMaterialReport maMaterialReport) {
        maMaterialReport.setId(IdUtil.getSnowflake(0, 0).nextIdStr());
        maMaterialReport.setCreateTime(DateUtils.getNowDate());
        maMaterialReport.setCreateBy(SecurityUtils.getUsername());
        return maMaterialReportMapper.insertMaMaterialReport(maMaterialReport);
    }

    /**
     * 修改物资月报
     *
     * @param maMaterialReport 物资月报
     * @return 结果
     */
    @Override
    public int updateMaMaterialReport(MaMaterialReport maMaterialReport) {
        maMaterialReport.setUpdateTime(DateUtils.getNowDate());
        maMaterialReport.setUpdateBy(SecurityUtils.getUsername());
        return maMaterialReportMapper.updateMaMaterialReport(maMaterialReport);
    }

    /**
     * 批量删除物资月报
     *
     * @param ids 需要删除的物资月报ID
     * @return 结果
     */
    @Override
    public int deleteMaMaterialReportByIds(String[] ids) {
        return maMaterialReportMapper.deleteMaMaterialReportByIds(ids);
    }

    /**
     * 删除物资月报信息
     *
     * @param id 物资月报ID
     * @return 结果
     */
    @Override
    public int deleteMaMaterialReportById(String id) {
        return maMaterialReportMapper.deleteMaMaterialReportById(id);
    }

    @Override
    public List<MaMaterialReport> selectReportList(String material, String date) {
        return maMaterialReportMapper.selectReportList(material, date);
    }

    @Override
    public List<MaterialReportVO> reportDataDeal(String material, String date) {

        //1. 查询物料信息
        List<MaMaterialReport> list = selectReportList(material, date );
        Map<String, List<MaMaterialReport>> mapList = list.stream().collect(Collectors.groupingBy(MaMaterialReport :: getMaterialId));
        List<MaterialReportVO> result = new ArrayList<>();
        mapList.forEach((key,item) ->{

            //区分A B区
            List<MaMaterialReport> aReport = item.stream().filter(o -> o.getParkNum().equals("A") ).collect(Collectors.toList());
            List<MaMaterialReport> bReport = item.stream().filter(o -> o.getParkNum().equals("B") ).collect(Collectors.toList());

            double lastBalanceA = aReport.stream().mapToDouble(MaMaterialReport::getLastBalance).sum();
            double nowBalanceA = aReport.stream().mapToDouble(MaMaterialReport::getNowBalance).sum();


            double lastBalanceB = bReport.stream().mapToDouble(MaMaterialReport::getLastBalance).sum();
            double nowBalanceB = bReport.stream().mapToDouble(MaMaterialReport::getNowBalance).sum();

            // 分离出不同仓库
            Map<String, List<MaMaterialReport>> awarehouse = aReport.stream().collect(Collectors.groupingBy(MaMaterialReport::getAddress));
            Map<String, List<MaMaterialReport>> bwarehouse = bReport.stream().collect(Collectors.groupingBy(MaMaterialReport::getAddress));
            awarehouse.forEach((address, d) ->{
                d.forEach(o ->{
                    MaterialReportVO vo = new MaterialReportVO();
                    vo.setWarehouse(address);
                    vo.setMaterialName(o.getMaterialCode());
                    vo.setStandards(o.getStandards());
                    vo.setUnit(o.getUnit());
                    vo.setLastBalanceA(lastBalanceA);
                    vo.setLastBalanceB(lastBalanceB);
                    vo.setNowBalanceA(nowBalanceA);
                    vo.setNowBalanceB(nowBalanceB);
                    vo.setInStorageNum(o.getInStorage());
                    if ("A".equals(o.getParkNum())){
                        vo.setOutStorageA(o.getOutStorage());
                    }else {
                        vo.setOutStorageB(o.getOutStorage());
                    }
                    vo.setLastBalanceTotal(vo.getLastBalanceA() + vo.getLastBalanceB());
                    vo.setNowBalanceTotal(vo.getNowBalanceA() + vo.getNowBalanceB());
                    result.add(vo);
                });

            });

            bwarehouse.forEach((address, d) ->{
                d.forEach(o ->{
                    MaterialReportVO vo = new MaterialReportVO();
                    vo.setWarehouse(address);
                    vo.setMaterialName(o.getMaterialCode());
                    vo.setStandards(o.getStandards());
                    vo.setUnit(o.getUnit());
                    vo.setLastBalanceA(lastBalanceA);
                    vo.setLastBalanceB(lastBalanceB);
                    vo.setNowBalanceA(nowBalanceA);
                    vo.setNowBalanceB(nowBalanceB);
                    vo.setInStorageNum(o.getInStorage());
                    if ("A".equals(o.getParkNum())){
                        vo.setOutStorageA(o.getOutStorage());
                    }else {
                        vo.setOutStorageB(o.getOutStorage());
                    }

                    vo.setLastBalanceTotal(vo.getLastBalanceA() + vo.getLastBalanceB());
                    vo.setNowBalanceTotal(vo.getNowBalanceA() + vo.getNowBalanceB());
                    result.add(vo);
                });

            });

        });












        return result;
    }
}
